/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResultsPanel.java
 *
 * Created on Feb 2, 2012, 4:56:53 PM
 */
package laboration2.gui;

import java.awt.CardLayout;
import java.util.List;
import javax.swing.JList;
import org.jdesktop.application.Action;
import se.chalmers.ait.dat215.lab2.Recipe;

/**
 *
 * @author arildm
 */
public class ResultsPanel extends javax.swing.JPanel {

    private CardLayout cardLayout;
    private int selectedIndex = -1;
    
    /** Creates new form ResultsPanel */
    public ResultsPanel() {
        initComponents();
        cardLayout = (CardLayout) getLayout();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fullListScrollPane = new javax.swing.JScrollPane();
        fullList = new javax.swing.JList();
        splitPane = new javax.swing.JSplitPane();
        shortListScrollPane = new javax.swing.JScrollPane();
        shortList = new javax.swing.JList();
        recipePanel = new javax.swing.JPanel();
        recipeToolbar = new javax.swing.JPanel();
        closeButton = new javax.swing.JButton();
        recipeView = new laboration2.gui.RecipeView();

        setName("Form"); // NOI18N
        setLayout(new java.awt.CardLayout());

        fullListScrollPane.setName("fullListScrollPane"); // NOI18N

        fullList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        fullList.setCellRenderer(new RecipeCell());
        fullList.setName("fullList"); // NOI18N
        fullList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listValueChanged(evt);
            }
        });
        fullListScrollPane.setViewportView(fullList);

        add(fullListScrollPane, "full");

        splitPane.setDividerLocation(100);
        splitPane.setName("splitPane"); // NOI18N

        shortListScrollPane.setName("shortListScrollPane"); // NOI18N

        shortList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        shortList.setCellRenderer(new RecipeCellShort());
        shortList.setName("shortList"); // NOI18N
        shortList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listValueChanged(evt);
            }
        });
        shortListScrollPane.setViewportView(shortList);

        splitPane.setLeftComponent(shortListScrollPane);

        recipePanel.setName("recipePanel"); // NOI18N
        recipePanel.setLayout(new java.awt.BorderLayout());

        recipeToolbar.setName("recipeToolbar"); // NOI18N
        recipeToolbar.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEADING));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(laboration2.Laboration2App.class).getContext().getActionMap(ResultsPanel.class, this);
        closeButton.setAction(actionMap.get("closeRecipe")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N
        recipeToolbar.add(closeButton);

        recipePanel.add(recipeToolbar, java.awt.BorderLayout.PAGE_END);

        recipeView.setName("recipeView"); // NOI18N
        recipePanel.add(recipeView, java.awt.BorderLayout.CENTER);

        splitPane.setRightComponent(recipePanel);

        add(splitPane, "split");
    }// </editor-fold>//GEN-END:initComponents

    private void listValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listValueChanged
        JList list = (JList) evt.getSource();
        Recipe recipe = (Recipe) list.getSelectedValue();
        
        if (list.getSelectedIndex() != selectedIndex && recipe != null) {
            selectedIndex = list.getSelectedIndex();
            // Show selected recipe
            recipeView.setRecipe(recipe);
            updateListSelection();
            cardLayout.show(this, "split");
            shortList.requestFocusInWindow();
        }
    }//GEN-LAST:event_listValueChanged

    @Action
    public void closeRecipe() {
        cardLayout.show(this, "full");
        fullList.requestFocusInWindow();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JList fullList;
    private javax.swing.JScrollPane fullListScrollPane;
    private javax.swing.JPanel recipePanel;
    private javax.swing.JPanel recipeToolbar;
    private laboration2.gui.RecipeView recipeView;
    private javax.swing.JList shortList;
    private javax.swing.JScrollPane shortListScrollPane;
    private javax.swing.JSplitPane splitPane;
    // End of variables declaration//GEN-END:variables

    public void setSearchResults(List<Recipe> recipes) {
        fullList.setListData(recipes.toArray());
        shortList.setListData(recipes.toArray());
        selectedIndex = -1;
    }

    private void updateListSelection() {
        shortList.setSelectedIndex(selectedIndex);
        fullList.setSelectedIndex(selectedIndex);
    }
}
